<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.7.16/dist/vue.js"></script>
    <div id="box">
        <input type="text" v-model="contents" placeholder="请输入新闻">
        <button v-on:click="more()">添加</button>
        <ul>
            <li v-for="(item,index) in arr" :key="item.id" v-color="index">{{item.name}}<button
                    v-on:click="dele(item.id)">删除</button>
            </li>
        </ul>
    </div>

    <script>
        Vue.directive("color", function (element, binding) {
            if (binding.value % 2 == 0) {
                element.style.backgroundColor = "red"
            }
        })
        new Vue({
            el: "#box",
            data: {
                arr: [
                    {
                        id: 1,
                        name: "新闻1"
                    },
                    {
                        id: 2,
                        name: "新闻2"
                    },
                    {
                        id: 3,
                        name: "新闻3"
                    }
                ],
                contents: ""
            },
            methods: {
                dele(i) {
                    var subbiao = this.arr.findIndex(item => item.id == i);
                    this.arr.splice(subbiao, 1)
                },
                more() {
                    this.arr.push({
                        id: this.arr.length + 1,
                        name: this.contents
                    })
                    this.contents=""
                }
            }
        })
    </script>
</body>

</html>